package com.zerox.controller;

import com.zerox.pojo.SysUser;
import com.zerox.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @author
 * @version V1.11
 * @ClassName:
 * @Description:
 * @Date
 */
@Controller
public class LoginController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/login")
    public String toLogin(){
        return "login";
    }

    @GetMapping("/list")
    public String toAdd(){
        return "list";
    }
    //处理登录请求
    @PostMapping("/login")
    public String doLogin(HttpSession session, Model model, @RequestParam(required = false) String username, @RequestParam(required = false)  String password){
        SysUser sys = sysUserService.login(username, password);

//        if(username.equals("admin") && password.equals("1111111")){
        if(sys!=null){
            session.setAttribute("loginUser",sys);
            return "list";
        }
        model.addAttribute("errorMsg","用户名或密码错误");
        return "error";
    }

    public LoginController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }
    
}
